// 抽离功能，可复用
// 使用 reactive 重构(X)

import {ref, onMounted, onUnmounted} from "vue";

function useMousePosition(){
  const x = ref(0)
  const y = ref(0)
  const updateMouse = (e: MouseEvent) => {
    x.value = e.pageX
    y.value = e.pageY
  }

  // 挂载
  onMounted(() => {
    document.addEventListener('click', updateMouse)
  })

  // 清除副作用
  onUnmounted(() => {
    document.removeEventListener('click', updateMouse)
  })

  return{
    x,
    y,
  }
}

// 导出
export default useMousePosition
